package Demo5;

public class MyThread extends Thread{
    private static int ticket = 0;
    static Object obj = new Object();// 锁对象要是唯一的如果是this那就不是唯一的
    @Override
    public void run() {
        // 卖100张票
        while (true) {
           synchronized (MyThread.class) {
               if (ticket < 100) {
                   try {
                       Thread.sleep(100);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
                   ticket++;
                   System.out.println(getName() +" 正在卖第" + ticket + "张票");
               } else {
                   break;
               }
           }
        }
    }
}
